package com.wei.czz.framework.admin.controller;

import com.wei.czz.common.annotation.Auth;
import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.dto.admin.roleSpace.RoleSpaceListDto;
import com.wei.czz.common.dto.admin.roleSpace.RoleSpacePageEnumDto;
import com.wei.czz.common.dto.common.NameValue;
import com.wei.czz.common.jsr303.group.Save;
import com.wei.czz.common.jsr303.group.Update;
import com.wei.czz.common.property.BatchProperty;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.vo.admin.roleSpace.RoleSpaceFormVo;
import com.wei.czz.common.vo.admin.roleSpace.RoleSpaceVo;
import com.wei.czz.common.vo.common.UpdateStatusVo;
import com.wei.czz.framework.admin.manager.RoleSpaceManager;
import com.wei.czz.framework.admin.service.RoleSpaceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-10-22 16:45:50
 * className: RoleSpaceController 系统角色授权组请求接口类
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/sys/role-space")
@AllArgsConstructor
public class RoleSpaceController {

    private static final Logger log = LoggerFactory.getLogger(RoleSpaceController.class);

    private final BatchProperty batchProperty;

    private final RoleSpaceManager roleSpaceManager;

    private final RoleSpaceService roleSpaceService;

    /**
     * 保存角色授权组接口
     * @param roleSpaceVo 参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'role-space-save' + #userPo.userId", message = "保存角色授权组")
    @PostMapping("/save")
    @Auth(permit = "system:role-space:save")
    public Result<Object> save(@Validated(Save.class) @RequestBody RoleSpaceVo roleSpaceVo) {
        log.info("开始保存角色授权组业务。{}", roleSpaceVo);

        roleSpaceService.save(roleSpaceVo);

        log.info("保存角色授权组业务结束");
        return Result.success();
    }

    /**
     * 分页获取角色授权组接口
     * @param formVo 参数对象
     * @return 结果
     */
    @PostMapping("/page/list")
    @Auth(permit = "system:role-space:list")
    public Result<PageDto<RoleSpaceListDto>> getPageList(@Validated @RequestBody RoleSpaceFormVo formVo) {
        log.info("开始分页获取角色授权组业务。{}", formVo);

        PageDto<RoleSpaceListDto> pageDto = roleSpaceManager.getPageList(formVo);

        log.info("分页获取角色授权组业务结束");
        return Result.success(pageDto);
    }

     /**
     * 获取角色授权组管理页面枚举接口
     * @return 结果
     */
    @GetMapping("/page/enum")
    @Auth(permit = "system:role-space:list")
    public Result<RoleSpacePageEnumDto> getPageEnum() {
        log.info("开始获取角色授权组管理页面枚举业务");

        RoleSpacePageEnumDto roleSpacePageEnumDto = roleSpaceManager.getPageEnum();

        log.info("获取角色授权组管理页面枚举业务结束");
        return Result.success(roleSpacePageEnumDto);
    }

    /**
     * 获取角色授权组简单列表接口
     * @return 结果
     */
    @GetMapping("/simple/list")
    @Auth(permit = "system:role-space:list")
    public Result<List<NameValue<Long>>> getSimpleList() {
        log.info("开始获取角色授权组列表业务");

        List<NameValue<Long>> list = roleSpaceService.getSimpleList();

        log.info("获取角色授权组列表业务结束");
        return Result.success(list);
    }

    /**
     * 修改角色授权组接口
     * @param roleSpaceVo 参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'role-space-update' + #userPo.userId", message = "修改角色授权组")
    @PostMapping("/update")
    @Auth(permit = "system:role-space:update")
    public Result<Object> update(@Validated(Update.class) @RequestBody RoleSpaceVo roleSpaceVo) {
        log.info("开始修改角色授权组业务。{}", roleSpaceVo);

        roleSpaceService.save(roleSpaceVo);

        log.info("修改角色授权组业务结束");
        return Result.success();
    }

    /**
     * 修改角色授权组状态接口
     * @param updateStatusVo 参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'role-space-update-status' + #userPo.userId", message = "修改角色授权组状态")
    @PostMapping("/update/status")
    @Auth(permit = "system:role-space:update")
    public Result<Object> updateStatus(@Validated @RequestBody UpdateStatusVo updateStatusVo) {
        log.info("开始修改角色授权组状态业务。{}", updateStatusVo);

        List<Long> idList = updateStatusVo.getIdList();
        if (batchProperty.getMaxSize() < idList.size()) {
            log.warn("修改角色授权组状态数量超限。batch.maxSize={} list.size={}", batchProperty.getMaxSize(), idList.size());
            return Result.clientFail("一次修改角色授权组状态，最多允许同时修改" + batchProperty.getMaxSize() + "个");
        }

        roleSpaceService.updateStatus(updateStatusVo);

        log.info("修改角色授权组状态业务结束");
        return Result.success();
    }

    /**
     * 删除角色授权组接口
     * @param idList 主键列表
     * @return 结果
     */
    @Resubmit(spEL = "'role-space-delete' + #userPo.userId", message = "删除角色授权组")
    @DeleteMapping("/delete")
    @Auth(permit = "system:role-space:delete")
    public Result<Object> delete(@RequestBody List<Long> idList) {
        log.info("开始删除角色授权组业务。idList={}", idList);
        if (idList.isEmpty()) {
            return Result.clientFail("主键列表不能为空");
        }
        if (batchProperty.getMaxSize() < idList.size()) {
            log.info("角色授权组删除条数超限。batch.size={} list.size={}", batchProperty.getMaxSize(), idList.size());
            return Result.clientFail("一次删除最多允许删除" + batchProperty.getMaxSize() + "条数据");
        }

        roleSpaceManager.delete(idList);

        return Result.success();
    }

}
